package com.zh.note.huawei.onehundred.相同数字的积木游戏;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Author: zhanghuan
 * @date: 2023/8/6 20:05
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int length = sc.nextInt();
        int[] values = new int[length];
        for (int i = 0; i < length; i++) {
            values[i] = sc.nextInt();
        }
        int res = solveMethod(values);
        System.out.println(res);

    }

    private static int solveMethod(int[] values) {

        int maxLen = -1;
        if (values.length == 0) {
            return maxLen;
        }
        Map<Integer, Integer> numCountMap = new HashMap<>();
        // 统计所有元素出现的次数
        for (int i : values) {
            numCountMap.put(i, numCountMap.getOrDefault(i, 0) + 1);
        }
        if (numCountMap.size() == values.length) { // 说明每个元素只出现了一次
            return maxLen;
        }
        for (Map.Entry<Integer, Integer> entry : numCountMap.entrySet()) {
            Integer number = entry.getValue();
            Integer count = entry.getValue();
            if (count < 2) { // 出现1次的跳过
                continue;
            }
            int low = 0;
            int high = values.length - 1;
            while (number != values[low]) {
                low++;
            }
            while (number != values[high]) {
                high--;
            }
            maxLen = Math.max(maxLen, high - low);
        }
        return maxLen;
    }
}
